package com.lightheart.sphr.xiaobinggan.kg.ui

import android.os.Bundle
import android.support.design.widget.NavigationView
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentTransaction
import android.support.v7.app.AppCompatActivity
import android.view.KeyEvent
import android.view.MenuItem
import android.widget.Toast
import com.blankj.utilcode.util.ToastUtils
import com.lightheart.sphr.xiaobinggan.R
import com.lightheart.sphr.xiaobinggan.fragment.FragmentOne
import com.lightheart.sphr.xiaobinggan.fragment.FragmentThree
import com.lightheart.sphr.xiaobinggan.fragment.FragmentTwo
import com.lightheart.sphr.xiaobinggan.utils.tablayout.TabLayout
import kotlinx.android.synthetic.main.activity_sideslip.*

class SideslipActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener, TabLayout.OnTabClickListener {

    private var mExitTime: Long = 0

    private var mCurrentFragment: Fragment? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sideslip)

        navigation_view.setNavigationItemSelectedListener(this)
        val tabs = ArrayList<TabLayout.Tab>() //fragment添加的地方
        tabs.add(TabLayout.Tab(R.drawable.selector_home, R.string.title_home, FragmentOne::class.java))
        tabs.add(TabLayout.Tab(R.drawable.selector_contract, R.string.title_contract, FragmentTwo::class.java))
        tabs.add(TabLayout.Tab(R.drawable.selector_my, R.string.title_my, FragmentThree::class.java))
        mTabLayout.setUpData(tabs, this)
        mTabLayout.setCurrentTab(0)

    }

    override fun onNavigationItemSelected(menuItem: MenuItem): Boolean {
        val title = menuItem.title as String
        Toast.makeText(this, "点击了----- $title", Toast.LENGTH_SHORT).show()
        return false
    }

    override fun onTabClick(tab: TabLayout.Tab) {
        try {
            var tmpFragment: Fragment = supportFragmentManager.findFragmentByTag(tab.targetFragmentClz.simpleName)
            val supportFragmentManager: FragmentManager = supportFragmentManager
            val transaction: FragmentTransaction = getSupportFragmentManager().beginTransaction()
            if (tmpFragment == null) {
                tmpFragment = tab.targetFragmentClz.newInstance()
                transaction.add(R.id.mFragmentContainerLayout, tmpFragment, tab.targetFragmentClz.simpleName)
                if (mCurrentFragment != null) {
                    transaction.hide(mCurrentFragment)
                }
                transaction.commit()
            } else {
                transaction.show(tmpFragment)
                if (mCurrentFragment != null) {
                    transaction.hide(mCurrentFragment)
                }
                transaction.commit()
            }
            mCurrentFragment = tmpFragment
        } catch (e: InstantiationException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - mExitTime > 2000) {
                ToastUtils.showLong("再按一次退出程序")
                mExitTime = System.currentTimeMillis()
            } else {
                finish()
            }
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}